NOTE: The Microsoft MSDN Site is incorrect in their example for C#! Use the below code..
[StructLayout(LayoutKind.Sequential)]
public struct SERVICE_STATUS
{
public int serviceType;
public int currentState;
public int controlsAccepted;
public int win32ExitCode;
public int serviceSpecificExitCode;
public int checkPoint;
public int waitHint;
}
public enum State
{
SERVICE_STOPPED = 0x00000001,
SERVICE_START_PENDING = 0x00000002,
SERVICE_STOP_PENDING = 0x00000003,
SERVICE_RUNNING = 0x00000004,
SERVICE_CONTINUE_PENDING = 0x00000005,
SERVICE_PAUSE_PENDING = 0x00000006,
SERVICE_PAUSED = 0x00000007,
}
[DllImport("advapi32.dll")]
private static extern bool SetServiceStatus(int hServiceStatus, ref SERVICE_STATUS lpServiceStatus);
protected override void OnStart(string[] args)
{
//NOTE: DO NOT USE THE RETURNED IntPtr.. Convert to INT or this will fail.
int handle = (int)this.ServiceHandle;
myServiceStatus.currentState = (int)State.SERVICE_START_PENDING;
SetServiceStatus(handle, ref myServiceStatus);
etc...
Declare Function SetServiceStatus Lib "advapi32.dll" (
ByVal hServiceStatus As Integer,
ByRef lpServiceStatus As SERVICE_STATUS)
As Integer
None.
None.